package com.ydd.controller.common;

import com.ydd.exception.BaseException;
import com.ydd.result.Result;
import com.ydd.service.CommonService;
import com.ydd.vo.ImgVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 通用接口
 */
@RestController
@RequestMapping("/common")
@Api(tags = "通用接口")
@Slf4j
public class CommonController {
    @Autowired
    private CommonService commonService;
    /**
     * 图片上传
     * @param
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public Result<ImgVO> upload(MultipartFile file)  {
        ImgVO imgVO = new ImgVO();
        try {
            imgVO.setMime("image/jpeg");
            imgVO.setName(file.getOriginalFilename());

            String url = commonService.upload(file);
            imgVO.setSavePath(url);
            //文件的请求路径
            imgVO.setSize(file.getSize());
        } catch (Exception e) {
            log.error("图片上传失败！");
            throw new BaseException("图片上传失败！");
        }
        return Result.success(imgVO);
    }
}